package com.trailwalker.JavaUtils.DateUtils;

import com.trailwalker.JavaUtils.string.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Trailwalker
 * @create 2020-07-12 18:21
 */
public class DateUtils {
    public static Date getDate() {
        return new Date();
    }

    public static String getDate(String format) {
        Date now = getDate();
        //可以方便地修改日期格式 如：yyyy/MM/dd HH:mm:ss
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(now);
    }

    public static String getDate(Date date) {
        //可以方便地修改日期格式 如：yyyy/MM/dd HH:mm:ss
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
        return dateFormat.format(date);
    }

    public static String getDate(Date date,String format) {
        //可以方便地修改日期格式 如：yyyy/MM/dd HH:mm:ss
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static Calendar getCalendar() {
        //可以对每个时间域单独修改
        return Calendar.getInstance();
    }


    public static String getDate(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + " days " + hours + " hours " + minutes + " minutes "
                + seconds + " seconds ";
    }

    /**
     * 获取Date实例
     *
     * @param time       "2019-01-03 10:59:27"
     * @param dateFormat "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static Date getDate(String time, String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取指定格式的时间
     *
     * @param time
     * @param dateFormat
     * @return
     */
    public static String getFormatDate(long time, String dateFormat) {
        if (dateFormat == null || dateFormat.isEmpty()) {
            dateFormat = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        return sdf.format(new Date(time));
    }

    public static String getFormatDate(long time) {
        return getFormatDate(time, ":", ":", "");
    }

    public static String getFormatDate(long time, String hourUnit, String miniteUnit, String secondUnit) {
        long second = time / 1000 % 60;
        long minite = time / 1000 / 60 % 60;
        long hour = time / 1000 / 60 / 60;
        StringBuilder stringBuilder = new StringBuilder();
        if (String.valueOf(hour).length() < 2) {
            stringBuilder.append("0");
        }
        stringBuilder.append(hour);
        if (StringUtils.isEmpty(hourUnit)) {
            stringBuilder.append(":");
        } else {
            stringBuilder.append(hourUnit);
        }
        if (String.valueOf(minite).length() < 2) {
            stringBuilder.append("0");
        }
        stringBuilder.append(minite);
        if (StringUtils.isEmpty(miniteUnit)) {
            stringBuilder.append(":");
        } else {
            stringBuilder.append(miniteUnit);
        }
        if (String.valueOf(second).length() < 2) {
            stringBuilder.append("0");
        }
        stringBuilder.append(second);
        if (!StringUtils.isEmpty(secondUnit)) {
            stringBuilder.append(secondUnit);
        }
        return stringBuilder.toString();
    }

    public static int getYear() {
        return getCalendar().get(Calendar.YEAR);
    }

    public static String getYear(Date date) {
        return getDate(date, "yyyy");
    }

    public static int getMonth() {
        return getCalendar().get(Calendar.MONTH);
    }

    public static String getMonth(Date date) {
        return getDate(date, "MM");
    }

    public static int getDay() {
        return getCalendar().get(Calendar.DATE);
    }

    public static String getDay(Date date) {
        return getDate(date, "dd");
    }

    public static int getHour_24() {
        return getCalendar().get(Calendar.HOUR_OF_DAY);
    }

    public static int getHour_12() {
        return getCalendar().get(Calendar.HOUR);
    }

    public static long getHour(long mss) {
        return (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
    }

    public static String getHour(Date date) {
        return getDate(date, "HH");
    }

    public static int getMinute() {
        return getCalendar().get(Calendar.MINUTE);
    }

    public static long getMinute(long mss) {
        return (mss % (1000 * 60 * 60)) / (1000 * 60);
    }

    public static String getMinute(Date date) {
        return getDate(date, "mm");
    }

    public static int getSecond() {
        return getCalendar().get(Calendar.SECOND);
    }

    public static long getSecond(long mss) {
        return (mss % (1000 * 60)) / 1000;
    }

    public static String getSecond(Date date) {
        return getDate(date, "ss");
    }

    public static long getMilliSecond(long mss) {
        return mss % (1000 * 60);
    }

    public static String getMilliSecond(Date date) {
        return getDate(date, "ssSSS");
    }

}
